package org.eerbaar.lang.expressions;

import org.eerbaar.lang.Expression;
import org.eerbaar.lang.HasRuntime;
import org.eerbaar.lang.Values;
import org.eerbaar.lang.runtime.Executor;

public class If implements Expression, HasRuntime {

    private Expression e;
    private Expression trueExpression = Values.TRUE;
    private Expression falseExpression = Values.FALSE;

    public If(Expression e) {
        this.e = e;
    }

    public org.eerbaar.lang.runtime.Runtime getRuntime() {

        return new org.eerbaar.lang.runtime.Runtime() {

            private Object result = Boolean.FALSE;

            @Override
            public void execute(Executor exec) {

                exec.execute(e);

                Object b = exec.getResult();
                if (b == Values.TRUE) {
                    exec.execute(trueExpression);
                    result = trueExpression;
                } else {
                    exec.execute(falseExpression);
                    result = falseExpression;
                }
            }

            @Override
            public Object getResult() {
                return result;
            }
        };
    }

}
